package com.x.control;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.x.service.transactional.ConfigurationService;

@Controller
@RequestMapping("/configuration")
public class ConfigController {
	@Autowired(required = true)
	ConfigurationService configurationService;

	String[] columns = { "fc", "shipMethod", "province", "city", "district",
			"tt", "shipments" };

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		int startIndex = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String draw = request.getParameter("draw");

		List<String[]> odrerPart = new ArrayList<>();
		List<String[]> wherePart = new ArrayList<>();

		for (int i = 0; i < columns.length; i++) {
			String orderColumn = request.getParameter("order[" + i
					+ "][column]");
			if (orderColumn != null) {
				String orderDir = request.getParameter("order[" + i + "][dir]");
				odrerPart.add(new String[] {
						columns[Integer.parseInt(orderColumn)], orderDir });
			}
			String searchable = request.getParameter("columns[" + i
					+ "][searchable]");
			if (searchable.equals("true")) {
				String searchColumn = columns[i];
				String value = request.getParameter("columns[" + i
						+ "][search][value]");
				if (!value.isEmpty()) {
					wherePart.add(new String[] { searchColumn, value });
				}
			}
		}

		Map<String, Object> result = configurationService.list(startIndex,
				length, wherePart, odrerPart);
		result.put("draw", draw);
		return result;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	ModelAndView create(@RequestParam("fc") String fc,
			@RequestParam("shipMethod") String shipMethod,
			@RequestParam("province") String province,
			@RequestParam("city") String city,
			@RequestParam("district") String district,
			@RequestParam("tt") String tt,
			@RequestParam("shipments") String shipments) {

		ModelAndView mv = new ModelAndView("home");
		mv.addObject("nav", "configuration");
		int transitTime = Integer.parseInt(tt);
		BigDecimal count = new BigDecimal(shipments);
		if (!configurationService.create(fc, shipMethod, province, city,
				district, transitTime, count)) {
			mv.addObject("error",
					"create basic configuration fail: Duplicate Configs!");
		}
		return mv;
	}

	@RequestMapping(value = "/suggest/province")
	@ResponseBody
	public List<String> suggestProvince() {
		List<String> result = new ArrayList<>();
		result = configurationService.getProvinceSuggest();
		return result;
	}

	@RequestMapping(value = "/suggest/city/{city}")
	@ResponseBody
	public List<String> suggestCity(@PathVariable(value = "city") String city) {
		List<String> result = new ArrayList<>();
		try {
			city = new String(city.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result = configurationService.getCitySuggest(city);
		return result;
	}

	@RequestMapping(value = "/loadCity")
	@ResponseBody
	public List<Object> loadCity(@RequestParam("province") String province) {
		return configurationService.loadCity(province);
	}

	@RequestMapping(value = "/suggest/district/{district}")
	@ResponseBody
	public List<String> suggestDistrict(
			@PathVariable(value = "district") String district) {
		List<String> result = new ArrayList<>();
		try {
			district = new String(district.getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result = configurationService.getDistrictSuggest(district);
		return result;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	String create(@RequestParam("id") String id, @RequestParam("fc") String fc,
			@RequestParam("shipMethod") String shipMethod,
			@RequestParam("province") String province,
			@RequestParam("city") String city,
			@RequestParam("district") String district,
			@RequestParam("tt") String tt,
			@RequestParam("shipments") String shipments) {
		int transitTime = Integer.parseInt(tt);
		BigDecimal count = new BigDecimal(shipments);
		configurationService.update(id, fc, shipMethod, province, city,
				district, transitTime, count);
		return "redirect:/home/configuration";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	String delete(@RequestParam("id") String id) {
		configurationService.delete(id);
		return "redirect:/home/configuration";
	}
	
}
